<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Base64 图像查看器</title>
    <link rel="stylesheet" href="../style/common.css" />
    <style>
      .action {
        user-select: none;
      }

      #result {
        padding: 10px;
        width: 100%;
        box-sizing: border-box;
        height: 3em;
      }

      #output {
        display: none;
      }
    </style>
  </head>
  <body>
    <div class="action">
      <button class="button button--primary" id="clear">清空</button>
    </div>

    <div>
      <textarea id="result" placeholder="Base64 编码" rows="10"></textarea>
    </div>
    <img id="output" src="#" />

    <script type="module">
      import { $ } from "../js/dom.js";
      import { copy } from "../js/clipboard.js";

      init();

      function init() {
        $("#clear").addEventListener("click", onClear);
        $("#result").addEventListener("input", onResultChanged);
      }

      function onClear() {
        $("#result").value = "";
      }

      function onResultChanged(e) {
        let raw = e.target.value;
        const prefix = "data:image/png;base64,";
        if (raw.indexOf(prefix) === -1) {
          raw = prefix + raw;
        }

        $("#output").src = raw;
        $("#output").style.display = "block";
      }
    </script>
  </body>
</html>
